import { Component, Injector, ViewEncapsulation, ViewChild, OnInit, EventEmitter, Output, ElementRef } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MaterialChargeServiceProxy, LocationServiceProxy, ReturnInventoryOfMaterialInput, AppLicationTreeDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'yl10ChildModal',
    templateUrl: './yl10-modal.component.html',
    encapsulation: ViewEncapsulation.None
})

export class YL10ChildModalComponent extends AppComponentBase {

    @ViewChild('secondModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    chargeId = '';
    outId = '';

    positionId = "";

    quantity = 0;
    remark = '';
    // areas = new Array<AppLicationTreeDto>();
    // area = new AppLicationTreeDto();
    pipeId = '';

    constructor(
        injector: Injector,
        private _returnMaterialServiceProxy: MaterialChargeServiceProxy,
        private _locationServiceProxy: LocationServiceProxy,
    ) {
        super(injector);
    }

    show(pipeId: string, outId: string): void {
        this.positionId = "";

        this.active = true;
        this.quantity = 0;
        this.remark = '';
        //this.chargeId = chargeId;
        this.outId = outId;
        this.pipeId = pipeId;
        this.modal.show();
        // this._locationServiceProxy.getAreas("", true, 51)
        // .subscribe(result => {
        //     this.areas = result.items;
        // });
        // setTimeout(() => {
        //
        // }, 0);
    }

    save(): void {
        if (!this.positionId) {
            this.notify.error('请选择一个位置');
            return;
        }
        if (!this.quantity) {
            this.notify.error('退库数量不能为0');
            return;
        }

        this.saving = true;
        var ret=new ReturnInventoryOfMaterialInput();
        ret.itemAmount=this.quantity;
        ret.locationId=this.positionId;
        ret.materialId=this.outId;
        this._returnMaterialServiceProxy.returnAsync(ret,this.pipeId)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.close();
                this.modalSave.emit(null);
                this.notify.success('退库成功');
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
